{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 4.3 \u4f7f\u7528\u751f\u6210\u5668\u521b\u5efa\u65b0\u7684\u8fed\u4ee3\u6a21\u5f0f\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u5b9e\u73b0\u4e00\u4e2a\u81ea\u5b9a\u4e49\u8fed\u4ee3\u6a21\u5f0f\uff0c\u8ddf\u666e\u901a\u7684\u5185\u7f6e\u51fd\u6570\u6bd4\u5982 range() , reversed() \u4e0d\u4e00\u6837\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u60f3\u5b9e\u73b0\u4e00\u79cd\u65b0\u7684\u8fed\u4ee3\u6a21\u5f0f\uff0c\u4f7f\u7528\u4e00\u4e2a\u751f\u6210\u5668\u51fd\u6570\u6765\u5b9a\u4e49\u5b83\u3002\n\u4e0b\u9762\u662f\u4e00\u4e2a\u751f\u4ea7\u67d0\u4e2a\u8303\u56f4\u5185\u6d6e\u70b9\u6570\u7684\u751f\u6210\u5668\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def frange(start, stop, increment):\n    x = start\n    while x < stop:\n        yield x\n        x += increment"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e3a\u4e86\u4f7f\u7528\u8fd9\u4e2a\u51fd\u6570\uff0c\n\u4f60\u53ef\u4ee5\u7528for\u5faa\u73af\u8fed\u4ee3\u5b83\u6216\u8005\u4f7f\u7528\u5176\u4ed6\u63a5\u53d7\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u7684\u51fd\u6570(\u6bd4\u5982 sum() , list() \u7b49)\u3002\u793a\u4f8b\u5982\u4e0b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "for n in frange(0, 4, 0.5):\n    print(n)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "list(frange(0, 1, 0.125))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e00\u4e2a\u51fd\u6570\u4e2d\u9700\u8981\u6709\u4e00\u4e2a yield \u8bed\u53e5\u5373\u53ef\u5c06\u5176\u8f6c\u6362\u4e3a\u4e00\u4e2a\u751f\u6210\u5668\u3002\n\u8ddf\u666e\u901a\u51fd\u6570\u4e0d\u540c\u7684\u662f\uff0c\u751f\u6210\u5668\u53ea\u80fd\u7528\u4e8e\u8fed\u4ee3\u64cd\u4f5c\u3002\n\u4e0b\u9762\u662f\u4e00\u4e2a\u5b9e\u9a8c\uff0c\u5411\u4f60\u5c55\u793a\u8fd9\u6837\u7684\u51fd\u6570\u5e95\u5c42\u5de5\u4f5c\u673a\u5236\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def countdown(n):\n    print('Starting to count from', n)\n    while n > 0:\n        yield n\n        n -= 1\n    print('Done!')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Create the generator, notice no output appears\nc = countdown(3)\nc"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Run to first yield and emit a value\nnext(c)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Run to the next yield\nnext(c)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Run to next yield\nnext(c)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Run to next yield (iteration stops)\nnext(c)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e00\u4e2a\u751f\u6210\u5668\u51fd\u6570\u4e3b\u8981\u7279\u5f81\u662f\u5b83\u53ea\u4f1a\u56de\u5e94\u5728\u8fed\u4ee3\u4e2d\u4f7f\u7528\u5230\u7684 next \u64cd\u4f5c\u3002\n\u4e00\u65e6\u751f\u6210\u5668\u51fd\u6570\u8fd4\u56de\u9000\u51fa\uff0c\u8fed\u4ee3\u7ec8\u6b62\u3002\u6211\u4eec\u5728\u8fed\u4ee3\u4e2d\u901a\u5e38\u4f7f\u7528\u7684for\u8bed\u53e5\u4f1a\u81ea\u52a8\u5904\u7406\u8fd9\u4e9b\u7ec6\u8282\uff0c\u6240\u4ee5\u4f60\u65e0\u9700\u62c5\u5fc3\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}